﻿#region Librerias

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ESF.SSO.Core.Entities;
using ESF.SSO.Presenter.Admin.Enterprise;
using ESF.Web.Pages;
using ESF.General;
using ESF.Web;
using ESF.SSO.Web.Tools.Modules;
using ESF.SSO.Presenter.Admin;
using ESF.SSO.Core.Entities.BasicReferences;

#endregion


public partial class Enterprise_EditEnterprise : BaseEditPage<EditEnterprisePresenter, IEditEnterpriseViewer, ESF.SSO.Core.Entities.Enterprise>, IEditEnterpriseViewer
{
	#region Carga Inicial
	
	protected void Page_Load(object sender, EventArgs e)
	{
		_Presenter = new EditEnterprisePresenter (this);
	
		if (!IsPostBack)
		{
			lblTitle.Text = Title;

			ListLoader.LoadList(ddlIdentificationType, WebGeneral.ListBasicReference(BasicReferenceTypes.IdentificationType));

			btnGoBack.Attributes["onclick"] = JS.GenerateGoToFinal("BrowseEnterprises.aspx");

			_Presenter.Load();
		}
	}
	
	#endregion


	#region IEditEnterpriseViewer Members

	public string Code
	{
		get
		{
			return txtCode.Text;
		}
		set
		{
			txtCode.Text = value;
		}
	}

	public string Name
	{
		get
		{
			return txtName.Text;
		}
		set
		{
			txtName.Text = value;
		}
	}

	public string IdentificationNumber
	{
		get
		{
			return txtIdentificationNumber.Text;
		}
		set
		{
			txtIdentificationNumber.Text = value;
		}
	}

	public string IdentificationNumberDigit
	{
		get
		{
			return txtIdentificationNumberDigit.Text;
		}
		set
		{
			txtIdentificationNumberDigit.Text = value;
		}
	}

	public int? IdentificationTypeID
	{
		get
		{
			return TextHelper.GetNullableInt (ddlIdentificationType.SelectedValue);
		}
		set
		{
			ListLoader.Select (ddlIdentificationType, value);
		}
	}

	public List<Terminal> Terminals
	{
		set
		{
			ListLoader.LoadList (chkTerminalList, value, "Code", "Code");
		}
		get
		{
			List<Terminal> terminals;


			terminals = new List<Terminal> ();

			foreach (ListItem terminalCode in chkTerminalList.Items)
			{
				terminals.Add (
					new Terminal () { 
						Code = terminalCode.Value, 
						Name = terminalCode.Value, 
						UserCreationAlias = frmUserAlias,
						CreationDateTime = DateTime.Now,
						BasicReferenceType = new ESF.General.Entity.BasicReferenceType () { ID = (int) BasicReferenceTypes.Terminals } });
			}

			return terminals;
		}
	}

	#endregion


	#region IBaseEditViewer Members

	public DateTime? CreationDateTime
	{
		set { uscAuditoryData.CreationDateTime = value; }
	}

	public DateTime? LastUpdateDateTime
	{
		set { uscAuditoryData.LastUpdateDateTime = value; }
	}

	public string UserCreationAlias
	{
		get
		{
			return frmUserAlias;
		}
		set
		{
			uscAuditoryData.UserCreationAlias = value;
		}
	}

	public string UserLastUpdateAlias
	{
		set { uscAuditoryData.UserLastUpdateAlias = value; }
	}

	#endregion
	
	
	#region IEditLocationViewer Members

	public Location Location
	{
		get
		{
			return uscEditLocation.Location;
		}
		set
		{
			uscEditLocation.Location = value;
		}
	}
	
	#endregion
	
	
	#region Metodos de Autocompletado

	[System.Web.Services.WebMethodAttribute(), System.Web.Script.Services.ScriptMethodAttribute()]
	public static string[] QueryCountriesByName (string prefixText, int count)
	{
		return DataHelper.ConvertToStringArray (GeneralAdmin.QueryBasicReferenceList(BasicReferenceTypes.Country, prefixText, count));
	}


	[System.Web.Services.WebMethodAttribute(), System.Web.Script.Services.ScriptMethodAttribute()]
	public static string[] QueryCitiesByName(string prefixText, int count)
	{
		return DataHelper.ConvertToStringArray(GeneralAdmin.QueryBasicReferenceList(BasicReferenceTypes.City, prefixText, count));
	}


	[System.Web.Services.WebMethodAttribute(), System.Web.Script.Services.ScriptMethodAttribute()]
	public static string[] QueryTerminals (string prefixText, int count)
	{
		return DataHelper.ConvertToStringArray (GeneralAdmin.QueryBasicReferenceList (BasicReferenceTypes.Terminals, prefixText, count));
	}
	
	#endregion
	

	#region Funciones Sobreescritas

	protected override Button GetSaveButton()
	{
		return btnSave;
	}


	protected override Button GetClearButton ()
	{
		return btnClear;
	}

	#endregion

	
	#region Eventos de los botones
	
	protected void btnAddTerminal_Click (object sender, EventArgs e)
	{
		ListLoader.AddItem (chkTerminalList, txtQueryTerminal.Text);

		txtQueryTerminal.Text = string.Empty;
	}

	
	protected void btnRemoveTerminal_Click (object sender, EventArgs e)
	{
		ListLoader.RemoveSelected (chkTerminalList);
	}

	#endregion
}
